﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

using SvnCore;

namespace SVNTreeViewer.Views
{
    /// <summary>
    /// Interaction logic for SvnDirectories.xaml
    /// </summary>
    public partial class SvnDirectories : Window
    {
        public SvnDirectories()
        {
            InitializeComponent();
            ////this.GetDirectoriesByPath();

            lbxPaths.Items.Add(new Uri("https://svn.kruk-inkaso.com.pl/svn/kruk/CRM/branches/CRM_2"));
            lbxPaths.Items.Add(new Uri("https://svn.kruk-inkaso.com.pl/svn/kruk/CRM/branches/CRM_2_R4"));
            lbxPaths.Items.Add(new Uri("https://svn.kruk-inkaso.com.pl/svn/kruk/CRM/trunk"));
        }

        public List<Uri> SvnDir
        {
            get
            {
                List<Uri> uris = new List<Uri>();
                foreach (Uri uri in lbxPaths.Items)
                {
                    uris.Add(uri);
                }

                return uris;
            }
        }

        private void ButtonClick(object sender, RoutedEventArgs e)
        {
            this.GetDirectoriesByPath();
        }

        private void GetDirectoriesByPath()
        {
            SvnFacade svn = new SvnFacade();
            foreach (Uri uri in svn.SvnGetDirectories(new Uri(this.txtRoothPath.Text)))
            {
                lbxPaths.Items.Add(uri);
            }
        }

        private void ButtonAddClick(object sender, RoutedEventArgs e)
        {
            Uri uri;
            if (Uri.TryCreate(txtCustomPath.Text, UriKind.RelativeOrAbsolute, out uri))
            {
                lbxPaths.Items.Add(uri);
            }
            else
            {
                MessageBox.Show("Invalid Uri:" + uri);
            }
        }

        private void GenerateClick(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
            this.Close();
        }

        private void OnDeleteDirectoryItem(object sender, ExecutedRoutedEventArgs e)
        {
            UIElement element = (UIElement)e.OriginalSource;
            ListBoxItem tryFindParent = element.TryFindParent<StackPanel>().TryFindParent<Border>().TryFindParent<ListBoxItem>();
            tryFindParent.IsSelected = true;
            this.lbxPaths.Items.Remove(this.lbxPaths.SelectedItem);
        }

        private void CanExecuteCustomCommand(object sender, CanExecuteRoutedEventArgs e)
        {
            Control target = e.Source as Control;

            if (target != null)
            {
                e.CanExecute = true;
            }
            else
            {
                e.CanExecute = false;
            }
        }
    }
}
